package com.example.demo.equipment.service.impl;

import com.example.demo.equipment.dao.CollectingDevice;
import com.example.demo.equipment.dao.CollectingSpace;
import com.example.demo.equipment.mapper.CollectingDeviceMapper;
import com.example.demo.equipment.mapper.CollectingSpaceMapper;
import com.example.demo.equipment.service.CollectingSpaceService;
import com.example.demo.system.utility.PageInfo;
import com.example.demo.system.utility.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CollectingSpaceServiceImpl implements CollectingSpaceService {

    @Autowired
    private CollectingSpaceMapper collectingSpaceMapper;
    @Autowired
    private CollectingDeviceMapper collectingDeviceMapper;

    @Override
    public Result getList(Integer pageNum, Integer pageSize, String name) {
        Integer start = PageInfo.start(pageNum, pageSize);
        List<CollectingSpace> list = collectingSpaceMapper.getList(start, pageSize, name);
        //获取每个空间下的设备集合
        for (CollectingSpace space : list) {
            space.setDeviceList(collectingDeviceMapper.getDeviceList(space.getId()));
        }
        Integer listCount = collectingSpaceMapper.getListCount(name);
        PageInfo info = new PageInfo(pageNum, pageSize, listCount, list);
        return Result.success(info);
    }

    @Override
    public CollectingSpace get(Integer id) {
        CollectingSpace space = collectingSpaceMapper.get(id);
        //获取空间下的设备集合
        space.setDeviceList(collectingDeviceMapper.getDeviceList(space.getId()));
        return space;
    }

    @Override
    public Result add(CollectingSpace collectingSpace) {
        collectingSpace.setModifyTime(new Date());
        collectingSpaceMapper.add(collectingSpace);
        //判断绑定设备集合是否为空
        if (null != collectingSpace.getDeviceList() && collectingSpace.getDeviceList().size() > 0) {
            //修改绑定id
            for (CollectingDevice device : collectingSpace.getDeviceList()) {
                device.setSpaceBind(collectingSpace.getId());//获取采集空间的主键
                collectingDeviceMapper.update(device);
            }
        }
        return Result.success("新增成功");
    }

    @Override
    public Result update(CollectingSpace collectingSpace) {
        collectingSpace.setModifyTime(new Date());
        collectingSpaceMapper.update(collectingSpace);
        //判断绑定设备集合是否为空
        if (null != collectingSpace.getDeviceList() && collectingSpace.getDeviceList().size() > 0) {
            //清空绑定id
            collectingDeviceMapper.updateSpaceBind(collectingSpace.getId());
            ////修改绑定id
            for (CollectingDevice device : collectingSpace.getDeviceList()) {
                device.setSpaceBind(collectingSpace.getId());//获取采集空间的主键
                collectingDeviceMapper.update(device);
            }
        }
        return Result.success("修改成功");
    }

    @Override
    public Result delete(Integer id) {
        collectingSpaceMapper.delete(id);
        return Result.success("删除成功");
    }
}
